#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "qpainter.h"
#include "qevent.h"
#include "qpoint.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    IsBlackTurn = true;
    resize(480,480);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *e)
{
    DrawChessboard();
    DrawItems();
    update();
    DrawItemWithMouse();
}

void MainWindow::DrawItems()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));

    for (int i = 0; i<Items.size(); i++)
    {
        Item item = Items[i];
        if (item.Black)
        {
            painter.setBrush(Qt::black);
        }
        else
        {
            painter.setBrush(Qt::white);
        }
        DrawChessAtPoint(painter,item.Pt);
    }
}

void MainWindow::DrawChessAtPoint(QPainter& painter,QPoint& pt)
{
    QPoint ptCenter((pt.x()+0.5)*30,(pt.y()+0.5)*30);
    painter.drawEllipse(ptCenter,12,12);
}
void MainWindow::DrawChessboard()
{
    QPainter painter(this);
    painter.setBrush(Qt::white);
    painter.setPen(QPen(QColor(Qt::black),2));

    for(int i = 0;i<15; i++)
    {
        for (int j = 0; j<15; j++)
        {
            painter.drawRect( (i+0.5)*30,(j+0.5)*30,30,30);
        }
    }
}

void MainWindow::DrawItemWithMouse()
{
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));
    if (IsBlackTurn)
    {
        painter.setBrush(Qt::black);
    }
    else
    {
        painter.setBrush(Qt::white);
    }
    painter.drawEllipse(mapFromGlobal(QCursor::pos()),12,12);
}

void MainWindow::mousePressEvent(QMouseEvent * e)
{
    QPoint pt;
    pt.setX( (e->pos().x() ) / 30);
    pt.setY( (e->pos().y() ) / 30);
    for (int i = 0; i<Items.size(); i++)
    {
        Item item = Items[i];
        if (item.Pt == pt)
        {
            return;
        }
    }
    Item item(pt,IsBlackTurn);
    Items.append(item);
    int nLeft =			CountNearItem(item,QPoint(-1,0));
    int nLeftUp =		CountNearItem(item,QPoint(-1,-1));
    int nUp =			CountNearItem(item,QPoint(0,-1));
    int nRightUp =		CountNearItem(item,QPoint(1,-1));
    int nRight =		CountNearItem(item,QPoint(1,0));
    int nRightDown =	CountNearItem(item,QPoint(1,1));
    int nDown =			CountNearItem(item,QPoint(0,1));
    int nLeftDown =		CountNearItem(item,QPoint(-1,1));
    if ( (nLeft + nRight) >= 4 ||
         (nLeftUp + nRightDown) >= 4 ||
         (nUp + nDown) >= 4 ||
         (nRightUp + nLeftDown) >= 4 )
    {
        QString str = IsBlackTurn?"Black":"White";
        Items.clear();
        return;
    }
    IsBlackTurn = !IsBlackTurn;
}

int MainWindow::CountNearItem(Item item,QPoint ptDirection)
{
    int nCount = 0;
    item.Pt += ptDirection;
    while (Items.contains(item))
    {
        nCount++;
        item.Pt += ptDirection;
    }
    return nCount;
}
